/*应用程序“开饭啦”的Controller模块*/
angular.module('kaifanlaController', [])
  .controller('startCtrl', function($scope,$location,$rootScope){
    $rootScope.jump = function(url){
      $location.path(url);
    }
    $scope.jumpToMain = function(){
      $location.path('/main')
    }
  })
  .controller('mainCtrl', function($scope, $http){
    //菜品搜索关键字
    $scope.kw = '';
    //监听搜索关键字的值的改变
    $scope.$watch('kw', function(){

      $http.get("/Index.php/Admin/Goods/getGood?kw="+$scope.kw)
        .success(function(data){
          if(data instanceof Array && data.length>0) {
            console.log(data);
            $scope.dishList = data;  //清除已有的记录，只显示此次查询到的记录
          }
        });
    });

    //服务器端是否还有更多的记录，默认为true
    $scope.hasMore = true;

    //控制器一加载立即请求前5条记录
    $http.get('/Index.php/Admin/Goods/getGood').success(function(data){
      console.log( data);
       $scope.dishList = data;
    });

    //“加载更多”按钮处理函数
    $scope.loadMore = function(){
      $http.get('/Index.php/Admin/Goods/getGood?start='+$scope.dishList.length)
        .success(function(data){
          if((data instanceof Array)  && data.length>0){
            $scope.dishList = $scope.dishList.concat(data);
          }else{
            console.log('获取数据失败！或没有更多数据了！');
            $scope.hasMore = false;
          }
        });
      }
  })
  .controller('detailCtrl', function($scope, $routeParams, $http,$location){
    $http.get('/Index.php/Admin/Goods/getGood?id='+$routeParams.dno).success(function(data){
      console.log($routeParams.dno)

      $scope.dish = data[0];
      //console.log($scope.dish[0])
    });
    $scope.jumpToMain = function(){
      $location.path('/main');
    }
    $scope.jumpToOrder = function(){
      $location.path('/order/'+$routeParams.dno);
    }
  })
  .controller('orderCtrl', function($scope,$routeParams,$http,$rootScope){
    $scope.formData = {did:$routeParams.dno};
    console.log( $scope.formData);
    $scope.submit = function(){
      $http.post(
        '/Index.php/Admin/Order/AddOrder',
        $.param($scope.formData),
        {headers:{'Content-Type':'application/x-www-form-urlencoded'}}
      ).success(function(data){
        console.log(data.status);
        if(data.status==1){
          $scope.succMsg = '订餐成功！您的订单编号为：'+data.oid+'。您可以在用户中心查看订单状态。'
          $rootScope.phone = $scope.formData.phone;
        }else {
          $scope.errMsg = '订餐失败！错误码为：'+data.result;
        }
      });
    }
  })
  .controller('myorderCtrl', function($scope,$routeParams,$http,$rootScope){
    $http.get('/Index.php/Admin/Order/GetOrder?phone=18607888537').success(function(data){
      $scope.orderList = data;
      console.log($scope.orderList );
    });
  })

